/*

文件名称:MMapWalk.js
文件描述:高德地图导航
设计作者:王敏
编码作者:王敏
完成日期:2015-8

调用routeSearch1方法
*/

  
var routeType,avoidName,rote; 
   
//计算规划为“多策略”时的驾车导航查询 
  
var coor=new Array(); 
  
function routeSearch1(lng1,lat1,lng2,lat2){ 
    //---起点与终点的经纬度
    var xy1=new MMap.LngLat(lng1,lat1);
    var xy2=new MMap.LngLat(lng2,lat2);
  
    //构建导航路线 
  
    var route = new MMap.RouteSearch({       
  
        routeType:9 
  
    }); 
  
    //驾车导航 
  
    route.getNaviPath([xy1,xy2],function(data){ 
  
        var resultStr=""; 
  
        var arr=new Array(); 
  
        //var name=new Array("速度优先","费用优先","距离优先"); 
         var name=new Array("速度优先","费用优先","距离优先"); 
        var bounds=dataEdit(data.list[0].bounds); 
  
        map.setBounds(new MMap.Bounds(bounds[0],bounds[1])); 
  
        for(var i=0;i<=2;i++){ 
  
            if(data.list[i].status === 'E0'){ 
  
                map.clearOverlays();  
  
                //显示导航路径信息 
  
                var s=new Array(); 
  
                var coors=new Array(); 
  
                coor[i]=new Array();//声明二维coor 
  
                s[i]="<div style=\"cursor:pointer;\" id=\"routeline_table"+(i)+"\"  onclick=\"opentable('"+(i)+"','"+(i)+"')\"><p>   ► "+name[i]+"</p></div><dl style=\"display:none\" id=\"routeline_id"+(i)+"\">",coors[i]='';  
  
                for(var j=0,l=data.list[i].count;j<l;j++){ 
  
                    s[i]+="<div id='divid" + i + j + "' onmouseover='showLineByIds(" + i + "," + j + ",this)' onmouseout='onmouseout_LineStyles(" + i + "," + j + ",this)' onclick='onclickLine(" + j + ",this)' style=\"font-size: 12px;cursor:pointer;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\"><table><tr><td>"+ (j+1) + "." + data.list[i].list[j].textInfo+"</td></tr></table></div>"; 
  
                    coors[i]+=data.list[i].list[j].coor+';'; 
  
                    coor[i][j]=data.list[i].list[j].coor; 
  
                } 
  
                s[i]+='</dl>'; 
  
               resultStr+=s[i]; 
  
                //在地图添加导航路线 
  

  
                        var polyline=new MMap.Polyline({ 
  
                            id:"polyline_all0", 
  
                            path:dataEdit(coors[i]), 
  
                            strokeColor:"red", 
  
                            strokeOpacity:0.8,  
  
                            strokeWeight:3   
  
                        }) 
  
                        arr.push(polyline); 
                        //鼠标移到上面显示路线信息
                         route = data.list[0];
               map.bind(polyline,"mouseover",function(){

                    if ($("#route_text").length > 0) {
                            $("#route_text").remove();
                         }
                        $("#text2").css("display","block");
            var route_text = "<table cellspacing='5 px'>";
            //步行导航路段数
            var steps = route.list;
            for (var i = 0; i < steps.length; i++) {
                route_text += "<tr><td colspan=2>" + i + "." + steps[i].textInfo + "</td></tr>";
            }
            route_text += "</table>";

            var parentDiv = $('<div id="route_text" style="position:absolute; bottom:10px; right:10px;background-color: #0D9BF2;color: #FFF;padding: 10px 10px 10px 10px;"></div>'); //创建一个父DIV
            parentDiv.html(route_text);
            parentDiv.appendTo('body'); //将父DIV添加到BODY中
        });

        map.bind(polyline, "mouseout", function() {
            if ($("#route_text").length > 0) {
                $("#route_text").remove();
            }
            $("#text2").css("display","none");

        });

                      
  
                if(i === 2){ 
  
                var marker1=new MMap.Marker({ 
  
                    id:'start', 
  
                     icon: 'http://webapi.amap.com/theme/v1.3/markers/n/starts.png',
  
                    offset:new MMap.Pixel(-8,-9), 
  
                    draggable:false, 
  
                    position:xy1 
  
                }); 
  
                var marker2=new MMap.Marker({ 
  
                    id:'end', 
  
                    icon: 'http://webapi.amap.com/theme/v1.3/markers/n/ends.png', 
  
                    offset:new MMap.Pixel(-8,-9), 
  
                    draggable:false, 
  
                    position:xy2 
  
                }); 
  
                arr.push(marker1); 
  
                arr.push(marker2); 
  
                map.addOverlays(arr); 
  
                } 
  
            }else if(data.list[i].status === 'E1'){     
  
                resultStr="<div style=\"cursor:pointer;\" id=\"routeline_table"+(i)+"\" onclick=\"opentable('"+(i)+"','"+(i)+"')\">   ► "+name[i]+"</div>未查找到任何结果!<br />建议：<br />1.请确保所有字词拼写正确。<br />2.尝试不同的关键字。<br />3.尝试更宽泛的关键字。"; 
  
            }else{ 
  
                if(data.list[i].error&&data.list[i].error.code&&data.list[i].error.description){ 
  
                    resultStr="<div style=\"cursor:pointer;\" id=\"routeline_table"+(i)+"\" onclick=\"opentable('"+(i)+"','"+(i)+"')\">   ► "+name[i]+"</div>错误信息："+data.list[i].error.code+','+data.list[i].error.description; 
  
                }else if(data.list[i].status){ 
  
                    resultStr="<div style=\"cursor:pointer;\" id=\"routeline_table"+(i)+"\" onclick=\"opentable('"+(i)+"','"+(i)+"')\">   ► "+name[i]+"</div>错误信息："+data.list[i].status+"请对照API Server v2.0.0 简明提示码对照表查找错误类型"; 
  
                } 
  
            } 
  
        } 
  
            var text1 = "<table cellspacing='5 px'><tr><td colspan=2>" + resultStr + "</td></tr></table>";
            var text2 = $('<div id="text2" style="display:none;position:absolute; bottom:10px; right:10px;background-color: #0D9BF2;color: #FFF;padding: 10px 10px 10px 10px;"></div>'); //创建一个父DIV
            text2.html(text1);
            text2.appendTo('body'); //将父DIV添加到BODY中

  
    }); 
  
} 
  
//展开结果列表 
  
function opentable(routelineID,num){   
  
    map.setFitView(); 
  
    var showid="routeline_table"+num; 
  
    var showids=document.getElementById(showid); 
  
    showids.style.display = (showids.style.display === "none"?"block":"none");  
  
    if(showids.style.display === "block") { 
  
        for(var i=0;i<3;i++){ 
  
            var id="routeline_table"+i; 
  
            document.getElementById(id).style.display="none"; 
  
        } 
  
        var showid="routeline_table"+num; 
  
        document.getElementById(showid).style.display=""; 
  
    } 
  
}   
  
//鼠标移动到结果列表某项时的变化(多策略） 
  
var polyline; 
  
function showLineByIds(i, j,thiss) { 
  
    thiss.style.background = '#CAE1FF'; 
  
    polyline = new MMap.Polyline({ 
  
        id:"polylines"+i+j, 
  
        path:dataEdit(coor[i][j]), 
  
        strokeColor:"#00FFFF",  
  
        strokeOpacity:0.8,   
  
        strokeWeight:3
  
    }); 
  
    map.addOverlays(polyline); 
  
} 
  
//鼠标离开结果列表的某项时恢复原状（多策略） 
  
function onmouseout_LineStyles(i, j,thiss) { 
  
    thiss.style.background = ""; 
  
    map.removeOverlays("polylines"+i+j);//删除覆盖物 
  
}    
  

//鼠标移动到结果列表某项时的变化 
  
var polyline; 
  
function showLineById(i,thiss) { 
  
    thiss.style.background = '#CAE1FF'; 
  
    polyline = new MMap.Polyline({ 
  
        id:"polyline"+i, 
  
        path:dataEdit(coor[i]), 
  
        strokeColor:"#00FFFF",  
  
        strokeOpacity:0.8,   
  
        strokeWeight:3  
  
    }); 
  
    map.addOverlays(polyline); 
  
} 
  
//鼠标离开结果列表的某项时恢复原状 
  
function onmouseout_LineStyle(i,thiss) { 
  
    thiss.style.background = ""; 
  
    map.removeOverlays("polyline"+i);//删除覆盖物 
  
} 
  
//鼠标点击结果列表的某项时打开marker的Tip 
  
function onclickLine(i,thiss) { 
  
    thiss.style.background = '#CAE1FF'; 
  
    map.setFitView([polyline]); 
  
} 

 //返回坐标数组 
  
function dataEdit(s){ 
  
    if(s.charAt(s.length-1) === ';'){ 
  
        s=s.substring(0,s.length-1); 
  
    } 
  
    var a=s.split(';'); 
  
    var b=[]; 
  
    for(var i=0,l=a.length;i<l;i++){ 
  
        var c=a[i].split(','); 
  
        b.push(new MMap.LngLat(c[0],c[1])); 
  
    } 
  
    return b; 
  
} 